<?php
/**
 * 设备盒子模型
 */

namespace app\api\model;

use app\api\model\Common;
use think\Validate;

class Equipment extends Common
{
    protected $rules = [
        'mac' => 'unique:equipment' 
    ];

    public function userhassn(){
        return $this->hasMany('UserHasSn', 'eid', 'equipmentid');
    }

    /**
     * 按条件搜索列表数据 
     * */
    public function getList($condition, $keyword, $page, $limit, $status)
    {
        try{
            $map = [
                'status' => $status
            ];
            $field = ['create_time','ip','status'];
            if($condition){
                $map[$condition] = ['like','%' . trim($keyword) . '%'];
            }
            $result = $this->where($map)->field($field, true)->limit($limit)->page($page)->order('type desc,update_time desc')->select();
            $total = $this->where($map)->count();
            $status = [1=>'正常',2=>'断开',3=>'异常'];
            $type = [1 => '终端', 2 => '录音笔', 3 => '校正端', 4=>'绘声字幕'];
            foreach($result as &$value){
                $value['connDetail'] = $value['type'] == 1 ? $status[$value['connection']] : "/";
                $value['typeDetail'] = $type[$value['type']];
                $value['version'] = $value['version'] ? : "/";
                $value['serial'] = $value['serial'] ? : "/";
                //$value['sn'] = $this->get($value['equipmentid'])->userhassn()->field('id,sn')->select();
            }
            return [
                'list' => $result,
                'total' => $total,
                'totalPage' => $limit != 0 ? ceil($total/$limit) : 0
            ];
        }catch(\Exception $e){
            throw new \Exception($e->getMessage());
        }
    }
    /**
     * 模糊查找匹配字段
     */
    public function fuzzySearchList($param, $match, $limit, $page)
    {
        try {
            $result = $this->where($param)->where('title|mac|serial', 'like', '%' . $match . '%')->limit($limit)->page($page)->order('update_time desc')->select();
            return $result;
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     *  通过属性获取一条
     *  参数 
     *  $param 符合数据库属性的键值数组 
     *  前端盒子使用函数
     */
    public function getDateBy($param)
    {
        try {
            $result = $this->where($param)->find();
            if (!$result) {
                return false;
            }
            return $result;
        } catch (\Exception $e) {
            //throw new \Exception("查找失败" . "," . $e->getMessage());
            return false;
        }
    }


    /**
     * 新建设备盒子
     * 参数 $param 符合数据库属性的键值数组 
     */
    public function createDates($param)
    {
        // 验证
        $validate = new Validate($this->rules);
        if (!$validate->check($param)) {
            $this->error = $validate->getError();
            return false;
        }
        try {
            $this->allowField(true)->data($param)->save();
            return true;
        } catch (\Exception $e) {
            $this->error = '添加失败';
            return false;
        }
    }

    /**
     * 修改设备盒子授权状态
     * 参数 $param  符合数据库属性的键值数组 
     *      $rules  自定义的更新验证规则
     *      $id     主键id
     */
    public function updateById($param, $id)
    {
        // 验证 独立验证status的值
            $validate = new Validate($this->rules);
            if (!$validate->check($param)) {
                echo $validate->getError();
                throw  new \Exception($validate->getError());
            }
        try {
            //按id更新status
            $this->allowField(true)->save($param, [$this->getPk() => $id]);
            return true;
        } catch (\Exception $e) {
            $this->error = '更新失败';
            throw  new \Exception('更新失败');
            return false;
        }
    }
}
